﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MemberExplanation
    {
        public int Add(Chain.Model.MemberExplanation model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemberExplanation(");
            builder.Append("MemberExplanationDesc,MemberExplanationTime)");
            builder.Append(" values (");
            builder.Append("@MemberExplanationDesc,@MemberExplanationTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberExplanationDesc", SqlDbType.NVarChar, 400), new SqlParameter("@MemberExplanationTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.MemberExplanationDesc;
            cmdParms[1].Value = model.MemberExplanationTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MemberExplanationID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemberExplanation ");
            builder.Append(" where MemberExplanationID=@MemberExplanationID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberExplanationID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemberExplanationID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MemberExplanationIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemberExplanation ");
            builder.Append(" where MemberExplanationID in (" + MemberExplanationIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int MemberExplanationID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemberExplanation");
            builder.Append(" where MemberExplanationID=@MemberExplanationID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberExplanationID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemberExplanationID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MemberExplanationID,MemberExplanationDesc,MemberExplanationTime ");
            builder.Append(" FROM MemberExplanation ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MemberExplanationID,MemberExplanationDesc,MemberExplanationTime ");
            builder.Append(" FROM MemberExplanation ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MemberExplanationID desc");
            }
            builder.Append(")AS Row, T.*  from MemberExplanation T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.MemberExplanation GetModel(int MemberExplanationID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MemberExplanationID,MemberExplanationDesc,MemberExplanationTime from MemberExplanation ");
            builder.Append(" where MemberExplanationID=@MemberExplanationID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberExplanationID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemberExplanationID;
            Chain.Model.MemberExplanation explanation = new Chain.Model.MemberExplanation();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MemberExplanationID"] != null) && (set.Tables[0].Rows[0]["MemberExplanationID"].ToString() != ""))
            {
                explanation.MemberExplanationID = int.Parse(set.Tables[0].Rows[0]["MemberExplanationID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberExplanationDesc"] != null) && (set.Tables[0].Rows[0]["MemberExplanationDesc"].ToString() != ""))
            {
                explanation.MemberExplanationDesc = set.Tables[0].Rows[0]["MemberExplanationDesc"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MemberExplanationTime"] != null) && (set.Tables[0].Rows[0]["MemberExplanationTime"].ToString() != ""))
            {
                explanation.MemberExplanationTime = DateTime.Parse(set.Tables[0].Rows[0]["MemberExplanationTime"].ToString());
            }
            return explanation;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemberExplanation ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MemberExplanation model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemberExplanation set ");
            builder.Append("MemberExplanationDesc=@MemberExplanationDesc,");
            builder.Append("MemberExplanationTime=@MemberExplanationTime");
            builder.Append(" where MemberExplanationID=@MemberExplanationID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberExplanationDesc", SqlDbType.NVarChar, 400), new SqlParameter("@MemberExplanationTime", SqlDbType.DateTime), new SqlParameter("@MemberExplanationID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MemberExplanationDesc;
            cmdParms[1].Value = model.MemberExplanationTime;
            cmdParms[2].Value = model.MemberExplanationID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

